package com.example.constellation.partnerfrag;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Spinner;

import com.example.constellation.R;
import com.example.constellation.bean.StarBean;
import com.example.constellation.utils.AssetsUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link PartnerFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class PartnerFragment extends Fragment  implements  View.OnClickListener, AdapterView.OnItemSelectedListener {
    ImageView manIv,womanIv;
    Spinner mansp,womansp;
    Button prizeBtn,analysisBtn;
    List<String> nameList;//存放了星座名称的集合

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private List<StarBean.StarinfoBean> infoList;
    private Map<String, Bitmap> contentlogoImgMap;

    public PartnerFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment PartnerFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static PartnerFragment newInstance(String param1, String param2) {
        PartnerFragment fragment = new PartnerFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        // Inflate the layout for this fragment
       View view=inflater.inflate(R.layout.fragment_partner, container, false);
       initView(view);

       //获取activity传递的数据
        Bundle bundle=getArguments();
        StarBean starBean= (StarBean) bundle.getSerializable("info");
        infoList = starBean.getStarinfo();
        nameList=new ArrayList<>();
        //获取适配器所需要的数据源
        for(int i=0;i<infoList.size();i++)
        {
            String name=infoList.get(i).getName();
            nameList.add(name);
        }
//创建适配器
        ArrayAdapter<String> adapter=new ArrayAdapter<>(getContext(),R.layout.item_partner_sp,R.id.item_partner_tv,nameList);
//设置适配器
        mansp.setAdapter(adapter);
        womansp.setAdapter(adapter);

        //获取第一个图片资源显示在imageview上
        String logoname=infoList.get(0).getLogoname();
        contentlogoImgMap = AssetsUtils.getContentlogoImgMap();
        Bitmap bitmap = contentlogoImgMap.get(logoname);
        manIv.setImageBitmap(bitmap);
        womanIv.setImageBitmap(bitmap);


        return view;
    }

    private void initView(View view) {
        manIv=view.findViewById(R.id.partnerfrag_iv_man);
        womanIv=view.findViewById(R.id.partnerfrag_iv_woman);
        mansp=view.findViewById(R.id.partnerfrag_sp_man);
        womansp=view.findViewById(R.id.partnerfrag_sp_woman);
        prizeBtn=view.findViewById(R.id.partnerfrag_btn_prize);
        analysisBtn=view.findViewById(R.id.partnerfrag_btn_analysis);


        //设置按钮的监听器
        prizeBtn.setOnClickListener(this);
        analysisBtn.setOnClickListener(this);
        mansp.setOnItemSelectedListener(this);
        womansp.setOnItemSelectedListener(this);





    }

    @Override
    public void onClick(View view) {
        switch (view.getId())
        {
            case R.id.partnerfrag_btn_prize:
                break;
            case R.id.partnerfrag_btn_analysis:
                //获取spinner选中的位置
                int mani = mansp.getSelectedItemPosition();

                int womani = womansp.getSelectedItemPosition();

                Intent intent=new Intent(getContext(),PartnerAnalysisActivity.class);
                intent.putExtra("man_name",infoList.get(mani).getName());
                intent.putExtra("man_logoname",infoList.get(mani).getLogoname());
                intent.putExtra("woman_name",infoList.get(womani).getName());
                intent.putExtra("woman_logoname",infoList.get(womani).getLogoname());
                startActivity(intent);
                break;
        }

    }

    //代码逻辑根据点击事件的触发 分为两个case
    //i是spinner的位置  根据这个位置可以找到名称   根据名称可以  存储在hashmap中bitmap的值  在进行设置
    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
        switch (adapterView.getId()) {
            case R.id.partnerfrag_sp_man:
                String logoname=infoList.get(i).getLogoname();
                Bitmap bitmap = contentlogoImgMap.get(logoname);
                manIv.setImageBitmap(bitmap);
                break;
            case R.id.partnerfrag_sp_woman:
                logoname=infoList.get(i).getLogoname();
                bitmap = contentlogoImgMap.get(logoname);
                womanIv.setImageBitmap(bitmap);

                break;
        }

    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {

    }
}